package org.sun.onepink

import android.os.Bundle
import android.view.GestureDetector
import android.view.Menu
import android.view.MotionEvent
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.navigation.NavigationView
import org.sun.onepink.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private lateinit var gestureDetector: GestureDetector
    private lateinit var drawerLayout: DrawerLayout
    private lateinit var navView: NavigationView
    private lateinit var textDebug: TextView // 用于调试显示的文本

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        // 初始化视图
        drawerLayout = binding.drawerLayout
        navView = binding.navView
        textDebug = findViewById(R.id.text_home) // 确保布局中有这个TextView

        // 设置导航控制器
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.nav_home, R.id.nav_gallery),
            drawerLayout
        )
        navView.setupWithNavController(navController)

        // 初始化手势检测器
        gestureDetector = GestureDetector(this, object : GestureDetector.SimpleOnGestureListener() {
            override fun onFling(
                e1: MotionEvent?, // 滑动起点
                e2: MotionEvent,  // 滑动终点
                velocityX: Float,  // X轴速度
                velocityY: Float   // Y轴速度
            ): Boolean {
                if (e1 == null) return false

                val deltaX = e2.x - e1.x
                val deltaY = e2.y - e1.y

                // 条件：右滑距离 > 垂直滑动，速度足够快，且从屏幕左侧1/4区域开始
                if (deltaX > Math.abs(deltaY) &&
                    velocityX > 800 &&
                    e1.x < resources.displayMetrics.widthPixels / 4) {

                    runOnUiThread {
                        if (!drawerLayout.isDrawerOpen(navView)) {
                            drawerLayout.openDrawer(navView) // 打开抽屉
                        }
                    }
                    return true // 消费事件
                }
                return false
            }
        })
    }

    // 关键：正确处理触摸事件分发
    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        ev?.let {
            if (gestureDetector.onTouchEvent(it)) {
                return true // 如果手势被消费，不再传递
            }
        }
        return super.dispatchTouchEvent(ev)
    }

    // 以下为默认生成的代码
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}